#include "dtwentyitemsmodel.h"
#include "modelitem.h"


#include <QMetaObject>
#include <QMetaProperty>
#include <QDebug>

DTwentyItemsModel::DTwentyItemsModel(QObject *parent) :
    QSortFilterProxyModel(parent),
    m_dataMetaObj(ModelItem::staticMetaObject)
{
    connect(this, &DTwentyItemsModel::layoutChanged, this, &DTwentyItemsModel::rowCountChanged);
    connect(this, &DTwentyItemsModel::rowsInserted, this, &DTwentyItemsModel::rowCountChanged);
    connect(this, &DTwentyItemsModel::rowsRemoved, this, &DTwentyItemsModel::rowCountChanged);

    // initialize sorting
    setSortRole(ItemTableModel::Role_SORT);
    sort(0);
    setDynamicSortFilter(true);
}

DTwentyItemsModel::DTwentyItemsModel(const QMetaObject &dataMetaObj, QObject *parent) :
    QSortFilterProxyModel(parent),
    m_dataMetaObj(dataMetaObj)
{
    connect(this, &DTwentyItemsModel::layoutChanged, this, &DTwentyItemsModel::rowCountChanged);
    connect(this, &DTwentyItemsModel::rowsInserted, this, &DTwentyItemsModel::rowCountChanged);
    connect(this, &DTwentyItemsModel::rowsRemoved, this, &DTwentyItemsModel::rowCountChanged);

    // initialize sorting
    setSortRole(ItemTableModel::Role_SORT);
    sort(0);
    setDynamicSortFilter(true);
}

QHash<int, QByteArray> DTwentyItemsModel::roleNames() const
{
    QHash<int, QByteArray> ret;
    QString itemName = m_dataMetaObj.className();
    itemName = itemName.replace(0, 1, itemName.left(1).toLower());
    ret.insert(Qt::DisplayRole, itemName.toLatin1());

    for (int propIndex = ModelItem::staticMetaObject.propertyOffset(); propIndex < m_dataMetaObj.propertyCount(); propIndex++)
    {
        QMetaProperty metaProp = m_dataMetaObj.property(propIndex);
        ret.insertMulti(DATAROLE_OFFSET+propIndex, metaProp.name());

        QByteArray propName = metaProp.name();
        propName = propName.replace(0, 1, propName.left(1).toUpper());
        QByteArray className = m_dataMetaObj.className();
        className = className.replace(0, 1, className.left(1).toLower());


        ret.insertMulti(DATAROLE_OFFSET+propIndex, className+propName);
    }

    return ret;
}

QVariant DTwentyItemsModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
    {
        return QVariant();
    }

    QModelIndex source = mapToSource(index);
    if (!source.isValid())
    {
        return QVariant();
    }

    ModelItem* item = reinterpret_cast<ModelItem*>(source.internalPointer());
    if (role == Qt::DisplayRole)
    {
        return QVariant::fromValue<ModelItem*>(item);
    }

    int propIndex = role - DATAROLE_OFFSET;

    if (propIndex >= 0)
    {
        QMetaProperty metaProp = m_dataMetaObj.property(propIndex);
        return metaProp.read(item);
    }

    return QVariant();
}


QObject *DTwentyItemsModel::get(int row) const
{
    QModelIndex proxy = index(row, 0);
    if (!proxy.isValid())
    {
        return 0;
    }

    QModelIndex source = mapToSource(proxy);
    if (!source.isValid())
    {
        return 0;
    }

    return reinterpret_cast<QObject*>(source.internalPointer());
}


